package com.like.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.like.entity.InfoGetReplyResp;
import com.like.entity.M;
import com.like.hospitalworld.R;
import com.like.utils.DateConverter;
import com.like.utils.ImageUtils;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

public class VideoCourseReplyAdapter extends BaseAdapter {

    public static final int TYPE_ASK = 0;
    public static final int TYPE_ANSWER = 1;

    private List<InfoGetReplyResp.InformationComment> mComments;
    private M m;

    public VideoCourseReplyAdapter(List<InfoGetReplyResp.InformationComment> comments, M m) {
        this.mComments = comments;
        this.m = m;
    }

    public void update(List<InfoGetReplyResp.InformationComment> comments, boolean append) {
        if (append)
            this.mComments.addAll(comments);
        else
            this.mComments = comments;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mComments.size();
    }

    @Override
    public InfoGetReplyResp.InformationComment getItem(int position) {
        return mComments.get(position);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
//        if (position % 2 == 0)
        return TYPE_ASK;
//        else
//            return TYPE_ANSWER;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        AskViewHolder askVh = null;
        AnswerViewHolder answerVh = null;
        if (convertView == null) {
            switch (type) {
                case TYPE_ANSWER:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_course_answer, parent, false);
                    answerVh = new AnswerViewHolder(convertView);
                    convertView.setTag(answerVh);
                    break;
                case TYPE_ASK:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_course_ask, parent, false);
                    askVh = new AskViewHolder(convertView);
                    convertView.setTag(askVh);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_ANSWER:
                    answerVh = (AnswerViewHolder) convertView.getTag();
                    break;
                case TYPE_ASK:
                    askVh = (AskViewHolder) convertView.getTag();
                    break;
            }
        }
        InfoGetReplyResp.InformationComment comment = getItem(position);
        switch (type) {
            case TYPE_ANSWER:
                break;
            case TYPE_ASK:
                askVh.sUsername.setText(comment.ickname);
                askVh.sContent.setText(comment.commentText);
                askVh.sTime.setText(DateConverter.convertShitToDate(comment.commentTime, "yyyy/MM/dd HH:mm"));
                ImageUtils.showImage(m.mContext, comment.head, m.getToken(), askVh.sUserIcon);
                break;
        }
        return convertView;
    }

    public class AskViewHolder {

        @ViewInject(R.id.user_icon)
        ImageView sUserIcon;
        @ViewInject(R.id.username)
        TextView sUsername;
        @ViewInject(R.id.content)
        TextView sContent;
        @ViewInject(R.id.time)
        TextView sTime;

        public AskViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }

    }

    public class AnswerViewHolder {

        @ViewInject(R.id.user_icon)
        ImageView sUserIcon;
        @ViewInject(R.id.username)
        TextView sUsername;
        @ViewInject(R.id.content)
        TextView sContent;

        public AnswerViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }
    }
}
